package scu.debate.web.controller;

import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import scu.debate.dao.entity.Arguement;
import scu.debate.dao.entity.Contest;
import scu.debate.service.contest.ContestService;
import scu.debate.web.utils.InterUtil;
import scu.debate.web.utils.ResultUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ContestController {
    @Autowired
    private ContestService contestService;

    @RequestMapping("/test")
    public String test(){
        return ResultUtils.successMessage("connection success!") ;
    }

    @RequestMapping(value = "getAllContest",method= RequestMethod.GET)
    public String getAllContest(){
        if (contestService.getAllContest() == null){
            return ResultUtils.errorMessage(null,"没有相关比赛信息");
        }
        return ResultUtils.successMessage(JSONArray.toJSONString(contestService.getAllContest()));
    }

    @RequestMapping(value = "addContest",method = RequestMethod.POST)
    public String addContest(@RequestBody HashMap<String,Object> map){
        Contest newContest = InterUtil.convertToContest(map);
        Contest contest = contestService.addContest(newContest);
        if(null == contest){
            return ResultUtils.errorMessage(null,"已经有了");
        }
        return ResultUtils.successMessage(JSONArray.toJSONString(contest));
    }

    @RequestMapping(value = "deleteContest",method = RequestMethod.POST)
    public String deleteContest(@RequestBody Map<String,Object> map){
        Integer contestId = (Integer) map.get("contestid");
        Contest contest = contestService.getContestById(contestId);
        if (contest == null){
            return ResultUtils.errorMessage(null,"没有这个比赛");
        }
        contestService.deleteContestById(contestId);
        return ResultUtils.successMessage(JSONArray.toJSONString(contest));
    }

    @RequestMapping(value = "updateContest",method = RequestMethod.POST)
    public String updateContest(@RequestBody HashMap<String,Object> map){
        Contest newContest = InterUtil.convertToContest(map);
        Contest contest = contestService.updateContest(newContest);
        if(contest == null){
            return ResultUtils.errorMessage(null,"比赛不存在");
        }
        return ResultUtils.successMessage(JSONArray.toJSONString(newContest));
    }

    @RequestMapping(value = "getContestById",method = RequestMethod.POST)
    public String getContestById(@RequestBody HashMap<String,Object> map){
        Integer contestid = (Integer) map.get("contestid");
        Contest contest = contestService.getContestById(contestid);
        if (null == contest){
            return ResultUtils.errorMessage(null,"没有这个比赛");
        }
        return  ResultUtils.successMessage(JSONArray.toJSONString(contest));
    }

    @RequestMapping(value = "getByArgumentId",method = RequestMethod.POST)
    public String getByArgumentId(@RequestBody Map<String,Object> map){
        Integer argumentId = (Integer) map.get("argumentId");
        if (contestService.getContestByArgumentId(argumentId)==null){
            return ResultUtils.errorMessage(null,"没有相关比赛信息");
        }
        return ResultUtils.successMessage(JSONArray.toJSONString(contestService.getContestByArgumentId(argumentId)));
    }

    @RequestMapping(value = "sortArguments",method = RequestMethod.POST)
    public String sortArguments(@RequestBody Map<String,String> map){
        String contestname = map.get("contestname");
        List<Arguement> arguments = contestService.getArgumentByCname(contestname);
        if (arguments.isEmpty()){
            return ResultUtils.errorMessage(null,"没有相关辩题");
        }
        return ResultUtils.successMessage(JSONArray.toJSONString(arguments));
    }

    @RequestMapping(value = "getAllContestname",method = RequestMethod.GET)
    public String getAllContestname(){
        List<String> contestNames = contestService.getAllContestname();
        if (contestNames.isEmpty()){
            return ResultUtils.errorMessage(null,"没有赛事信息");
        }
        return ResultUtils.successMessage(JSONArray.toJSONString(contestNames));
    }
}
